//
//  SignInViewController.m
//  项目三
//
//  Created by rimi1 on 2016/11/30.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "SignInViewController.h"

#import "SignInView.h"
#import "EnrollView.h"
#import "Alert.h"

#import "AVRequest+SignIn.h"

@interface SignInViewController () <SignInViewDelegate, EnrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) SignInView *signInView;
@property (nonatomic, strong) EnrollView *enrollView;

@end

@implementation SignInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeAppearance];
}

#pragma mark - initialize

- (void)initializeDataSource {
    
}

- (void)initializeAppearance {
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.signInView];
    [self.scrollView addSubview:self.enrollView];
    [self.navigationBar setHidden:YES];
    [self.signInView addSubview:self.navigationBar.left];
    [self.navigationBar.left setImage:[UIImage imageNamed:@"黑色返回"] forState:UIControlStateNormal];
}

#pragma mark - URLRequest
// 登录
- (void)urlSignInRequest {
    [AVRequest avSignInWithAccount:self.signInView.account.text password:self.signInView.password.text signIn:^(NSArray *objects, NSError *error) {
        if (error) {
            [Alert alertWithTitle:@"登录失败" message:@""];
            return;
        }
        
        if (objects.count == 0) {
            [Alert alertWithTitle:@"登录失败" message:@"该账号不存在"];
        }else {
            [MyUser initUserWithObj:objects.firstObject];
            if (self.signInSucceed) {
                self.signInSucceed();
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    }];
}
// 注册
- (void)urlEnrollRequestWithAccount:(NSString *)account password:(NSString *)password name:(NSString *)name sex:(NSString *)sex {
    [AVRequest avEnrollWithAccount:account password:password name:name sex:sex avSucceed:^(BOOL succeed, NSError *error) {
        if (error) {
            [Alert alertWithTitle:@"注册失败" message:@""];
            return;
        }
        
        if (succeed) {
            [Alert alertWithTitle:@"注册成功" message:@"" sure:^{
                [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
                [self.enrollView enrollSucceed];
            }];
        }else {
            [Alert alertWithTitle:@"注册失败" message:@""];
        }
    }];
}
// 登录的一些条件判断
- (void)signInWith {
    NSString *acc = self.signInView.account.text;
    NSString *pas = self.signInView.password.text;
    if (acc.length < 6 || pas.length < 6) {
        // 格式错误
        [Alert alertWithTitle:@"登录失败" message:@"请输入正确格式"];
    }else {
        [self urlSignInRequest];
    }
}

#pragma mark - respondsTo...

- (void)respondsToTap:(UITapGestureRecognizer *)gesture {
    [self.view endEditing:YES];
}

#pragma mark - SignInViewDelegate
// 100：返回  101：登录  102：注册  103：忘记
- (void)signInView:(SignInView *)signInView tapOfButton:(UIButton *)sender {
    switch (sender.tag) {
        case 100:{
            [self dismissViewControllerAnimated:YES completion:nil];
        }
            break;
            
        case 101:{
            [self signInWith];
        }
            break;
            
        case 102:{
            [self.scrollView setContentOffset:CGPointMake(kWIDTH, 0) animated:YES];
        }
            break;
            
        case 103:{
            [Alert alertWithTitle:@"温馨提示" message:@"请联系管理人员" sure:nil];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - EnrollViewDelegate

- (void)enrollView:(EnrollView *)enrollView tapOfSureWithIs:(BOOL)is account:(NSString *)account password:(NSString *)password name:(NSString *)name sex:(NSString *)sex {
    // 真则条件通过，创建账号
    if (is) {
        [self urlEnrollRequestWithAccount:account password:password name:name sex:sex];
    }else {
        [Alert alertWithTitle:@"注册失败" message:@"格式错误"];
    }
}

#pragma mark - get

- (UIScrollView *)scrollView {
    
    if (!_scrollView) {
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, kHEIGHT)];
        _scrollView.contentSize = CGSizeMake(2 * kWIDTH, 0);
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.backgroundColor = [UIColor whiteColor];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(respondsToTap:)];
        [_scrollView addGestureRecognizer:tap];
        
    }
    return _scrollView;
}

- (SignInView *)signInView {
    
    if (!_signInView) {
        
        _signInView = [[SignInView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, kHEIGHT)];
        _signInView.delegate = self;
        
    }
    return _signInView;
}

- (EnrollView *)enrollView {
    
    if (!_enrollView) {
        
        CGFloat width = kWIDTH - 2 * WXQAdaptionWidth(20);
        _enrollView = [[EnrollView alloc] initWithFrame:CGRectMake(kWIDTH + (kWIDTH - width)/2, 34, width, 470)];
        _enrollView.layer.cornerRadius = 5;
        _enrollView.layer.masksToBounds = YES;
        _enrollView.layer.borderWidth = 1;
        _enrollView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _enrollView.delegate = self;
        
    }
    return _enrollView;
}

@end














